#include <string.h>
#include "Util.h"

int Util::StrEQ(const string& p, const string& q)
{
	if (p.size() != q.size()) {
		return 0;
	}	

	if (strcmp(p.c_str(), q.c_str()) == 0) {
		return 1;
	}

	return 0;	
}

int Util::IsSpace(char c)
{
	if (c == '\f' || c == '\n' || c == '\t' || c == '\r' || c == '\v')
		return 1;

	return 0;		
}

void Util::Trim(string& str)
{
	if (str.empty()) return;
	
	size_t f = 0, e = str.size() - 1;
	while( IsSpace( str[f] ) ) f++;
	while( IsSpace( str[e] ) && e >= f) e--;

	str = str.substr(f, e-f+1);
}

void Util::Explode(const string& str, vector<string>& vec, const string delim)
{
	if (str.empty()) return;

	string temp;
	size_t f = 0, e = 0;
	while((e = str.find(delim, f)) != string::npos) {
		temp = str.substr(f, e-f);
		Trim(temp);
		if(!temp.empty()) vec.push_back(temp);
		f = e + 1;				
	}			
	temp = str.substr(f, str.size()-f);
	Trim(temp);
	if (!temp.empty()) vec.push_back(temp);	
}

void Util::Implode(const vector<string>& vec, string& str, const string delim)
{
	if (vec.empty()) return;

	size_t i;
	for (i=0; i<vec.size()-1; ++i) {
		str += vec[i];
		str += delim;					
	}	
	str += vec[i];
}
